
package gui;

import java.awt.Color;
import java.awt.Rectangle;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import client.Client;
import gui.MainPanel;
import javax.swing.JFrame;
import java.awt.Dimension;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridBagLayout;
import javax.swing.JLabel;
import java.awt.Rectangle;
import java.util.Vector;

import javax.swing.ImageIcon;
import javax.swing.JComboBox;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JProgressBar;
import client.Client;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;

/*
 * this class helps the employ add a comlaint . he can also click first to see the user 
 * details before he commite the the complaint so it will be a vaild user name 
 * 
 */
public class Complaint extends JFrame {
	
	
	
	private JPanel Sub = null;
	private MainEmployee ME;
	private JButton jButtonBack = null;

	public static String[] complain =null;
	//public static String[] compid =null;
	public static String[] det =null;
	public static String[] userde =null;
	final public static int DEFAULT_PORT = 5555;  
    private String host = MainPanel.ip; //  @jve:decl-index=0:
    int port=0;
	public static Client client1;
	public static Client client2;
	public static Client client3;
	public static String[] ans;
	public static String[] compans=new String[2];
	public static String[] chackname;
	public static String[] chackname1;
	private JTextField jTextFieldusername = null;
	private JLabel jLabeluser = null;
	private JLabel jLabelfristname = null;
	private JTextField jTextFieldfname = null;
	private JLabel jLabellastname = null;
	private JTextField jTextFieldlname = null;
	private JLabel jLabeladd = null;
	private JTextField jTextFieldAddress = null;
	private JButton jButtonsearch = null;  //  @jve:decl-index=0:visual-constraint="537,98"
	private JLabel jLabeldesc = null;
	private JButton jButtonNext1 = null;
	private JLabel jLabelBackground = null;
	private JTextArea jTextAreadesc = null;
	private JButton jButtonHelp = null;
	public Complaint() {
		super();
		
		initialize();
	}

	/**
	 * This method initializes AcademicFrame 
	 */
	private void initialize() {
		this.setSize(1000, 720);
		this.setContentPane(getSub());
	
		
	//	client1 = new Client(host,DEFAULT_PORT);
	//	det = new String[2];
	//	det[0]=MainPanel.Userinfo[1];
	//	client1.accept(det);
	//		Object p = client1.getMsg();
	//		ans = (String[])p;
		
		this.setTitle("CREATE COMPLAINT");		
	}

	private JPanel getSub() {
		if (Sub == null) {
			ImageIcon imageBack = new ImageIcon("comp.jpg");
			jLabelBackground = new JLabel();
			jLabelBackground.setIcon(imageBack);
			jLabelBackground.setBounds(new Rectangle(1, -1, 990, 687));						 							
			jLabeldesc = new JLabel();
			jLabeldesc.setBounds(new Rectangle(63, 206, 172, 42));
			jLabeldesc.setText("complaint description:");
			jLabeladd = new JLabel();
			jLabeladd.setBounds(new Rectangle(485, 135, 59, 21));
			jLabeladd.setText("Address:");
			jLabellastname = new JLabel();
			jLabellastname.setBounds(new Rectangle(251,133, 78, 22));
			jLabellastname.setText("Last name:");
			jLabelfristname = new JLabel();
			jLabelfristname.setBounds(new Rectangle(29, 133, 78, 22));
			jLabelfristname.setText("First name:");
			jLabeluser = new JLabel();
			jLabeluser.setBounds(new Rectangle(91, 60, 163, 37));
			jLabeluser.setText("Enter the name of the user:");
			Sub = new JPanel();
			Sub.setLayout(null);
			Sub.setBackground(Color.CYAN);
			Sub.add(getJButtonBack(), null);
		
			Sub.add(getJTextFieldusername(), null);
			Sub.add(jLabeluser, null);
			Sub.add(jLabelfristname, null);
			Sub.add(getJTextFieldfname(), null);
			Sub.add(jLabellastname, null);
			Sub.add(getJTextFieldlname(), null);
			Sub.add(jLabeladd, null);
			Sub.add(getJTextFieldAddress(), null);
			Sub.add(getJButtonsearch(), null);
			Sub.add(jLabeldesc, null);
			Sub.add(getJTextAreadesc(), null);
			Sub.add(getJButtonNext1(), null);
			Sub.add(getJButtonHelp(), null);
			Sub.add(jLabelBackground, null);
			
			
			

		}
		return Sub; 
	}

	/**
	 * This method initializes jButtonBack	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonBack() {
		if (jButtonBack == null) {
			jButtonBack = new JButton();
			jButtonBack.setBounds(new Rectangle(260, 560, 143, 68));
			jButtonBack.setText("BACK");
			jButtonBack.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					ME = new MainEmployee();
					ME.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
					ME.setSize( 1000,720 ); // set frame size
					ME.setResizable(true);
					ME.setVisible( true ); // display frame
					Complaint.this.dispose();
				    
				}
			});
		}
		return jButtonBack;
	}

	/**
	 * This method initializes jButtonNext	
	 * 	
	 * @return javax.swing.JButton	
	 */
	
	/**
	 * This method initializes jTextFieldusername	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextFieldusername() {
		if (jTextFieldusername == null) {
			jTextFieldusername = new JTextField();
			jTextFieldusername.setBounds(new Rectangle(306, 58, 203, 39));
			//jTextFieldusername.setText(MainPanel.Userinfo[3]);
		}
		return jTextFieldusername;
	}

	/**
	 * This method initializes jTextFieldfname	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextFieldfname() {
		if (jTextFieldfname == null) {
			jTextFieldfname = new JTextField();
			jTextFieldfname.setBounds(new Rectangle(118, 132, 118, 24));
		}
		return jTextFieldfname;
	}

	/**
	 * This method initializes jTextFieldlname	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextFieldlname() {
		if (jTextFieldlname == null) {
			jTextFieldlname = new JTextField();
			jTextFieldlname.setBounds(new Rectangle(340, 132, 118, 24));
		}
		return jTextFieldlname;
	}

	/**
	 * This method initializes jTextFieldAddress	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextFieldAddress() {
		if (jTextFieldAddress == null) {
			jTextFieldAddress = new JTextField();
			jTextFieldAddress.setBounds(new Rectangle(557, 132, 118, 24));
		}
		return jTextFieldAddress;
	}

	/**
	 * This method initializes jButtonsearch	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonsearch() {
		if (jButtonsearch == null) {
			jButtonsearch = new JButton();
			jButtonsearch.setBounds(new Rectangle(520, 50, 246, 68));
			jButtonsearch.setText("Click here to watch user details");
			jButtonsearch.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
				/*
				 * the listener check if the user name exist in our system ,
				 * if he is in the system so the fildes of the details r filled
				 * and if not , the msg will anform the employ that the user name dose not exist in the system
				 *  
				 */
					String name=jTextFieldusername.getText();
					int s=0;
				   
				    if(!(jTextFieldusername.getText()==null)){
				    	 client3 = new Client(host,DEFAULT_PORT);
							    chackname1=new String[5];
								chackname1[0]="chacknamevalid";
								chackname1[1]=jTextFieldusername.getText();
								client3.accept(chackname1);
								
								Object t = client3.getMsg();
								chackname1 = (String[])t;
								int chak=Integer.parseInt(chackname1[1]);
								

								
								
								
								if((chak==0)){
									Closecomsec close = new Closecomsec();
									close.jLabelMassage.setText("user name is not exist");
									close.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
									close.setSize( 380,217 ); // set frame size
									close.setResizable(true);
									close.setVisible( true ); 
									 s =1;
								}
								
								}
								
					if(((name!=null)&&(s==0))){
					    client1 = new Client(host,DEFAULT_PORT);
					    userde = new String[2];
					    userde[0] = "userdet";						
					    userde[1] = name;
					    client1.accept(userde);
						Object p = client1.getMsg();
						ans = (String[])p;
						
						if (ans[0].equals("2"))
						{	
							ErrorFrame EFrame = new ErrorFrame();
							EFrame.jLabelMassage.setText("user name is not in the system");
							EFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
							EFrame.setSize( 380,217 ); // set frame size
							EFrame.setResizable(true);
							EFrame.setVisible( true ); 
						}
					 
						
						if (ans[0].equals("1"))
						{
							jTextFieldfname.setText(ans[1]);
							jTextFieldlname.setText(ans[2]);
							jTextFieldAddress.setText(ans[3]);
							
						}
						
					 
					   
					}
				
				    
				}
			});
		}
		return jButtonsearch;
	}

	/**
	 * This method initializes jButtonNext1	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonNext1() {
		if (jButtonNext1 == null) {
			jButtonNext1 = new JButton();
			jButtonNext1.setBounds(new Rectangle(707, 560, 143, 68));
			jButtonNext1.setText("NEXT");
			jButtonNext1.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					/*
					 * the listener check if the user name exist in our system ,
					 * if he is in the system so the fildes of the details r filled
					 * and if not , the msg will inform the employ that the user name dose not exist in the system
					 *  then it takes the description filled and create a new complaint 
					 *  for the user name that the employ wrote
					 *  
					 */
					int o=3;
	    String desc=jTextAreadesc.getText();
	    if(!(jTextFieldusername.getText()==null)){
	    	 client3 = new Client(host,DEFAULT_PORT);
				    chackname=new String[5];
					chackname[0]="chacknamevalid";
					chackname[1]=jTextFieldusername.getText();
					client3.accept(chackname);
					
					Object t = client3.getMsg();
					chackname = (String[])t;
					if(chackname[1]=="0"){
						Closecomsec close = new Closecomsec();
						close.jLabelMassage.setText("user name is not exist");
						close.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
						close.setSize( 380,217 ); // set frame size
						close.setResizable(true);
						close.setVisible( true ); 
						 o =1;
					}
					
					}
	  
	    
	    String de="";
	    if((desc.equalsIgnoreCase(de))){
	    	
			o=1;
	    }
	    if((o==3)&&((jTextAreadesc.getText())!=null)){
						
					    client2 = new Client(host,DEFAULT_PORT);
					    complain = new String[5];
					    complain[0] = "complaint";
						String name1=jTextFieldusername.getText();
						complain[1] = name1;
					    complain[2] = name1;
					    complain[3] = desc;
					    complain[4] = MainPanel.Userinfo[3];
					    client2.accept(complain);
						Object s = client2.getMsg();
						compans = (String[])s;
					    Cominssec added = new Cominssec();
						 added.jLabelMassage.setText("complaint added");
						 added.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
						 added.setSize( 380,217 ); // set frame size
						 added.setResizable(true);
						 added.setVisible( true );  
						 jTextFieldusername.setText("");
						 jTextFieldfname.setText("");
						 jTextFieldlname.setText("");
						 jTextFieldAddress.setText("");
						 jTextAreadesc.setText("");
					   
					}
	    if(o==1){
	    Closecomsec close = new Closecomsec();
	    
		close.jLabelMassage.setText("description filed is empty");
		close.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
		close.setSize( 380,217 ); // set frame size
		close.setResizable(true);
		close.setVisible( true ); 
				
	    }	    
				}
			});
		}
		return jButtonNext1;
	}

	/**
	 * This method initializes jTextAreadesc	
	 * 	
	 * @return javax.swing.JTextArea	
	 */
	private JTextArea getJTextAreadesc() {
		if (jTextAreadesc == null) {
			jTextAreadesc = new JTextArea();
			jTextAreadesc.setBounds(new Rectangle(260, 211, 375, 115));
		}
		return jTextAreadesc;
	}

	/**
	 * This method initializes jButtonHelp	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonHelp() {
		if (jButtonHelp == null) {
			jButtonHelp = new JButton();
			jButtonHelp.setBounds(new Rectangle(100, 560, 143, 68));
			jButtonHelp.setText("HELP");
			jButtonHelp.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					HelpFrame HFrame = new HelpFrame();

					HFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
					HFrame.setSize( 600,720 ); // set frame size
					HFrame.setResizable(false);
					HFrame.setVisible( true ); // display frame
				}
			});
		}
		return jButtonHelp;
	}
	
}  //  @jve:decl-index=0:visual-constraint="12,11"

